package com.jhgsys.internal.accident.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.accident.entity.AccidentAlertsFile;
import com.jhgsys.internal.accident.service.IAccidentAlertsFileService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 安全警报附件 Controller
 *
 * @author zhangwei
 * @date 2024-08-23 13:28:12
 */
@Slf4j
@Validated
@Controller
public class AccidentAlertsFileController extends BaseController {

    @Autowired
    private IAccidentAlertsFileService accidentAlertsFileService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentAlertsFile")
    @RequiresPermissions("accidentAlertsFile:view")
    public String accidentAlertsFileIndex(){
        return FebsUtil.view("accidentAlertsFile/accidentAlertsFileList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentAlertsFile/add")
    @RequiresPermissions("accidentAlertsFile:add")
    public String accidentAlertsFileAdd(){
        return FebsUtil.view("accidentAlertsFile/accidentAlertsFileEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentAlertsFile/update/{id}")
    @RequiresPermissions("accidentAlertsFile:edit")
    public String accidentAlertsFileUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("accidentAlertsFile", accidentAlertsFileService.findAccidentAlertsFileById(id));
        return FebsUtil.view("accidentAlertsFile/accidentAlertsFileEdit");
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     */
    @GetMapping("accidentAlertsFile")
    @ResponseBody
    @RequiresPermissions("accidentAlertsFile:view")
    public FebsResponse getAllAccidentAlertsFiles(AccidentAlertsFile accidentAlertsFile) {
        return new FebsResponse().success().data(accidentAlertsFileService.findAccidentAlertsFileList(accidentAlertsFile));
    }

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     */
    @GetMapping("accidentAlertsFile/list")
    @ResponseBody
    @RequiresPermissions("accidentAlertsFile:view")
    public FebsResponse accidentAlertsFileList(QueryRequest request, AccidentAlertsFile accidentAlertsFile) {
        Map<String, Object> dataTable = getDataTable(this.accidentAlertsFileService.findAccidentAlertsFilePage(request, accidentAlertsFile));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 AccidentAlertsFile
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     */
    @Log("新增AccidentAlertsFile")
    @PostMapping("accidentAlertsFile")
    @ResponseBody
    @RequiresPermissions("accidentAlertsFile:add")
    public FebsResponse addAccidentAlertsFile(@Valid AccidentAlertsFile accidentAlertsFile) throws FebsException {
        try {
            accidentAlertsFile.setCreatedBy(getCurrentUser().getUserName());
            this.accidentAlertsFileService.createAccidentAlertsFile(accidentAlertsFile);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增AccidentAlertsFile失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 AccidentAlertsFile
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     */
    @Log("逻辑删除AccidentAlertsFile")
    @GetMapping("accidentAlertsFile/delete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentAlertsFile:delete")
    public FebsResponse deleteAccidentAlertsFile(@PathVariable("id") String accidentAlertsFileIds) throws FebsException {
        try {
            AccidentAlertsFile accidentAlertsFile = new AccidentAlertsFile();
            accidentAlertsFile.setDeletedBy(getCurrentUser().getUserName());
            accidentAlertsFile.setDeletedTime(new Date());
            accidentAlertsFile.setDeletedFlg(FebsConstant.INVALID);
            this.accidentAlertsFileService.deleteAccidentAlertsFile(accidentAlertsFile,accidentAlertsFileIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentAlertsFile失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 AccidentAlertsFile
    * @author zhangwei
    * @date 2024-08-23 13:28:12
    */
    @Log("物理删除AccidentAlertsFile")
    @GetMapping("accidentAlertsFile/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentAlertsFile:delete")
    public FebsResponse physicallyDeleteAccidentAlertsFile(@PathVariable("id") String accidentAlertsFileIds) throws FebsException {
        try {
            this.accidentAlertsFileService.physicallyDeleteAccidentAlertsFile(accidentAlertsFileIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentAlertsFile失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 AccidentAlertsFile
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     */
    @Log("修改AccidentAlertsFile")
    @PostMapping("accidentAlertsFile/update")
    @ResponseBody
    @RequiresPermissions("accidentAlertsFile:edit")
    public FebsResponse updateAccidentAlertsFile(AccidentAlertsFile accidentAlertsFile) throws FebsException {
        try {
            accidentAlertsFile.setUpdatedBy(getCurrentUser().getUserName());
            this.accidentAlertsFileService.updateAccidentAlertsFile(accidentAlertsFile);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改AccidentAlertsFile失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 导出 AccidentAlertsFile的excel
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     */
    @PostMapping("accidentAlertsFile/excel")
    @ResponseBody
    @RequiresPermissions("accidentAlertsFile:view")
    public void export(QueryRequest queryRequest, AccidentAlertsFile accidentAlertsFile, HttpServletResponse response) throws FebsException {
        try {
            List<AccidentAlertsFile> accidentAlertsFiles = this.accidentAlertsFileService.findAccidentAlertsFilePage(queryRequest, accidentAlertsFile).getRecords();
            ExcelKit.$Export(AccidentAlertsFile.class, response).downXlsx(accidentAlertsFiles, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
